package com.liaoye.hellocustomview.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.view.View;

public class BitMapCreateView extends View {
    private Paint mPaint;
    private Bitmap mDestBitmap;

    public BitMapCreateView(Context context) {
        super(context);
    }

    private void initColor(int width, int height) {
        int[] colors = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int r = x * 255 / (width - 1);
                int g = y * 255 / (width - 1);
                int b = 255 - Math.min(r, g);
                int a = Math.max(r, g);
                colors[y * width + x] = Color.argb(r, g, b, a);
            }
        }
    }

    private void init() {
        mPaint = new Paint();

        int width = 500;
        int height = 300;

        mDestBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(mDestBitmap);
        Paint paint = new Paint();
        final LinearGradient linearGradient = new LinearGradient(width / 2, 0, width / 2, height, 0xffffffff, 0x00ffffff, Shader.TileMode.CLAMP);
        mPaint.setShader(linearGradient);
        canvas.drawRect(0, 0, width, height, paint);


    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(mDestBitmap, 0, 0, mPaint);
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        canvas.drawRect(0, 0, mDestBitmap.getWidth(), mDestBitmap.getHeight(), mPaint);
    }
}
